<?php

session_start();
//session_destroy();

$PageLoad = new PageLoad;
$PageLoad -> Main();

class PageLoad {

	var $realUserName = "admin";
	var $realPassword = "password";
	var $loginStatus = "notLoggedIn";
	var $loggedInStatus;

	var $lastUsername = "";
	var $errorMessage;
	var $notLoggedIn = "Ej inloggad";
	var $loggedIn = "Inloggad";

	var $inputField;

	var $currentDate;

	var $form = "<form name='login' id='login' method='POST'>
				Användarnamn: <input type='text' name='username' id='username' value='' class='text' maxlenght='20' /></br>
				Lösenord: <input type='password' name='password' id='password' class='text' maxlenght='20' /></br>
				Håll mig inloggad: <input type='checkbox' name='keepLoggedIn' value='Bike'></br>
				<input type='submit' value='Logga in' />
				</form>";	var $logoutButton = "<form name='logout' id='logout' method='POST'><input type='submit' value='Logga ut'</input></form>";
	public function Main() {

		$this -> login();
		$this -> setFields();
		$this -> currentDate = $this -> setClock();
		$this -> renderHTML();
	}

	public function login() {
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST' && $_SESSION['loggedIn'] == 'true') {
			$_SESSION['loggedIn'] = 'false';
			$this -> loginStatus = "justLoggedOut";
		} elseif ($_SESSION['loggedIn'] == 'true') {
			$this -> loginStatus = "loggedIn";
		} elseif (isset($_POST['username']) && isset($_POST['password'])) {
			if ($_POST['username'] == "Admin" && $_POST['password'] == "Password") {
				$this -> loginStatus = "justLoggedIn";
				$_SESSION['loggedIn'] = 'true';
			} elseif ($_POST['username'] != "" && $_POST['password'] == "") {
				$this -> loginStatus = "missingPassword";
			} elseif ($_POST['username'] == "" && $_POST['password'] != "") {
				$this -> loginStatus = "missingUsername";
			} elseif ($_POST['username'] == "Admin" && $_POST['password'] != "Password") {
				$this -> loginStatus = "wrongUsernameOrPassword";
			} elseif ($_POST['username'] != "Admin" && $_POST['password'] == "Password") {
				$this -> loginStatus = "wrongUsernameOrPassword";
			} elseif ($_POST['username'] == "" && $_POST['password'] == "") {
				$this -> loginStatus = "missingUsername";
			}
		} else {
			$this -> loginStatus = "notLoggedIn";
		}
	}

	public function setFields() {
		$this -> lastUsername = "";
		$this -> loggedInStatus = "";
		$this -> errorMessage = "";
		$this -> inputField = "";
		switch ($this -> loginStatus) {
			case 'notLoggedIn' :
				$this -> loggedInStatus = "<h2>Ej Inloggad</h2>";
				$this -> inputField = $this -> form;
				break;
			case 'justLoggedIn' :
				$this -> loggedInStatus = "<h2>Admin är Inloggad</h2>";
				$this -> errorMessage = "Inloggning lyckades";
				$this -> inputField = $this -> logoutButton;
				break;
			case 'loggedIn' :
				$this -> loggedInStatus = "<h2>Admin är Inloggad</h2>";
				$this -> inputField = $this -> logoutButton;
				break;
			case 'justLoggedOut' :
				$this -> loggedInStatus = "<h2>Ej Inloggad</h2>";
				$this -> errorMessage = "Du har nu loggat ut";
				$this -> inputField = $this -> form;
				break;
			case 'missingUsername' :
				$this -> loggedInStatus = "<h2>Ej Inloggad</h2>";
				$this -> errorMessage = "Användarnamn saknas";
				$this -> inputField = $this -> form;
				break;
			case 'missingPassword' :
				$this -> lastUsername = $_POST['username'];
				$this -> loggedInStatus = "<h2>Ej Inloggad</h2>";
				$this -> errorMessage = "Lösenord saknas";
				$this -> inputField = $this -> form;
				break;
			case 'wrongUsernameOrPassword' :
				$this -> lastUsername = $_POST['username'];
				$this -> loggedInStatus = "<h2>Ej Inloggad</h2>";
				$this -> errorMessage = "Fel användarnamn och/eller lösenord";
				$this -> inputField = $this -> form;
				break;
			case 'default' :
				break;
		}
	}

	public function renderHTML() {
		if ($this -> loginStatus == "notLoggedIn" || $this -> loginStatus == "justLoggedOut" || $this -> loginStatus == "missingUsername" || $this -> loginStatus == "missingPassword" || $this -> loginStatus == "wrongUsernameOrPassword") {
			echo "<!DOCTYPE HTML>
		<html>
			<head>
				<title>Laboration. Inte inloggad</title>
				<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
			</head>
			<body><h1> Laboration ep222fj </h1>" . $this -> loggedInStatus . "
				<br><p> " . $this -> errorMessage . " </p>
				<form name='login' id='login' method='POST'>
					Användarnamn: <input type='text' name='username' id='username' value='" . $this -> lastUsername . "' class='text'><br>
					Lösenord: <input type='password' name='password' id='password' class='text'><br>
					Håll mig inloggad: <input type='checkbox' name='keepLoggedIn' value='Bike'><br>
					<input type='submit' value='Logga in' />
				</form>
				<p>
				<br> " . $this -> currentDate . "
				</p>
			</body>
		</html>";
		} else {
			echo "<!DOCTYPE HTML>
		<html>
			<head>
				<title>Laboration. Inloggad</title>
				<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
			</head>				
			<body> 
				<h1> Laboration ep222fj </h1><p>" . $this -> loggedInStatus . "
				<br> " . $this -> errorMessage . "
				<br> " . $this -> inputField . "
				<br> " . $this -> currentDate . "
				</p>
			</body>
		</html>";
		}
	}

	public function setClock() {
		setlocale(LC_TIME, 'sv_SE', 'sv_SE.UTF-8', 'Swedish_Sweden.1252', 'WINDOWS-1252');
		//$this -> currentDate = strftime("%A, den %d %B år %Y. Klockan är [%H:%M:%S]");
		return strftime("%A, den %d %B år %Y. Klockan är [%H:%M:%S]");
	}

}
